﻿using Guest_room.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;


namespace Guest_room.employee
{
    public partial class shcart : Form
    {
        Guest_roomDataContext db = new Guest_roomDataContext();
        public shcart()
        {
            InitializeComponent();
        }
        //创建适配器
        SqlDataAdapter Dachart;
        DataSet ds = new DataSet();//适配器对象
        GuestRoomService guestRoomService = new GuestRoomService();
        void init()
        {
            string userId = Login.userid ?? Home.userid;
            Dachart = guestRoomService.GetUserCart(userId);
            Dachart.Fill(ds, "chart");
            dataGridView1.DataSource = ds.Tables["chart"];
        }

        void showAll()
        {
            DataView dvchart = new DataView(ds.Tables["chart"]);
            dataGridView1.DataSource = dvchart;
            //字体居中
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列名居中
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列宽自适应列名
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            //隐藏用户编号列
            dataGridView1.Columns["入住经办员编号"].Visible = false;
            dataGridView1.Columns["用户编号"].Visible = false;
            dataGridView1.Columns["结算状态"].Visible = false;
            dataGridView1.Columns["创建时间"].Visible = false;
        }


        private void shcart_Load(object sender, EventArgs e)//窗体加载函数
        {
            // 获取用户ID
            string userId = Login.userid ?? Home.userid;
            // 查询用户购物车表
            var cartItems = guestRoomService.GetUserShoppingCartItems(userId);

            // 检查购物车是否为空
            if (!cartItems.Any())
            {
                this.Dispose();
                MessageBox.Show("您的购物车为空！");
                roin roin = new roin();
                roin.ShowDialog();
            }
            else
            {
                init();
                showAll();
                showAll();
            }
        }

        private void button3_Click(object sender, EventArgs e)//关闭
        {
            this.Dispose();
            roin roin = new roin();
            roin.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count == 0)
            {
                MessageBox.Show("购物车为空！");
                return;
            }

            string[] roomNumbers = new string[dataGridView1.Rows.Count - 1];
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                roomNumbers[i] = dataGridView1.Rows[i].Cells[3].Value.ToString();
            }

            try
            {
                guestRoomService.ClearShoppingCart(roomNumbers, (Login.userid ?? Home.userid));
                MessageBox.Show("清空成功！");
                this.Dispose();
                roin roin = new roin();
                roin.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//数据表
        {
            //双击数据表中的某一行，弹出窗体修改该行数据的入住天数的值
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridViewRow selectedRow = dataGridView1.Rows[e.RowIndex];
                string roomNumber = selectedRow.Cells["客房编号"].Value.ToString();
                int currentStayDays = Convert.ToInt32(selectedRow.Cells["入住天数"].Value);
                string roomtime = selectedRow.Cells["入住日期"].Value.ToString();
                int peoples = Convert.ToInt32(selectedRow.Cells["入住人数"].Value);

                // 创建并显示修改入住天数的窗体
                var form = new UpdateStayDaysForm(roomNumber, currentStayDays, roomtime, peoples);
                form.ShowDialog();

                // 在窗体关闭后，更新数据表中的入住天数值
                if (form.DialogResult == DialogResult.OK)
                {
                    UpdateStayDaysForm.StayInfo stayInfo = form.GetStayDays();
                    if (stayInfo.Days == -1 || stayInfo.Number == -1 || stayInfo.CheckInTime == DateTime.MinValue)
                    {
                        return;
                    }
                    else if (stayInfo.Days == currentStayDays && stayInfo.Number == peoples && stayInfo.CheckInTime == DateTime.Parse(roomtime))
                    {
                        showAll();
                    }
                    else
                    {
                        int newStayDays = stayInfo.Days;
                        int newpeoples = stayInfo.Number;
                        DateTime newroomtime = stayInfo.CheckInTime;

                        selectedRow.Cells["入住天数"].Value = newStayDays;
                        selectedRow.Cells["入住人数"].Value = newpeoples;
                        selectedRow.Cells["入住日期"].Value = newroomtime;
                        selectedRow.Cells["更新时间"].Value = DateTime.Now;
                        // 更新数据表中的总价值
                        selectedRow.Cells["订单总价"].Value = Convert.ToInt32(selectedRow.Cells["客房单价"].Value) * newStayDays;
                        peoples = Convert.ToInt32(selectedRow.Cells["入住人数"].Value);
                        roomtime = selectedRow.Cells["入住日期"].Value.ToString();
                        currentStayDays = Convert.ToInt32(selectedRow.Cells["入住天数"].Value);
                        string roomupdatetime = selectedRow.Cells["更新时间"].Value.ToString();
                        int roomtotalprice = Convert.ToInt32(selectedRow.Cells["订单总价"].Value);

                        //DB.GetCn();
                        //string str = "update 用户购物车表 set 入住人数='" + peoples + "',入住日期='" + roomtime + "',入住天数='" + currentStayDays + "',更新时间='" + roomupdatetime + "',订单总价='" + roomtotalprice + "' where 客房编号='" + roomNumber + "' and 入住经办员编号='" + (Login.userid ?? Home.userid) + "'";
                        //DB.sqlEx(str);
                        string userid = (Login.userid ?? Home.userid);

                        guestRoomService.UpdateUserCart(roomNumber, userid, peoples, roomtime, 
                            currentStayDays, roomupdatetime, roomtotalprice);

                        MessageBox.Show("修改成功！");
                    }
                }
                showAll();
            }

        }

        private void button2_Click(object sender, EventArgs e)//结算
        {
            // // 获取用户购物车表基本信息
            // List<用户购物车表> cartList = (from c in db.用户购物车表
            //                          where c.入住经办员编号 == (Login.userid ?? Form1.userid)
            //                          select c).ToList();

            // // 根据用户购物车表基本信息生成订单头，状态为未审核
            // 客房订单表 order = new 客房订单表();
            // order.订单状态 = "未审核";

            // // 根据用户购物车表基本信息生成订单明细
            // foreach (用户购物车表 cart in cartList)
            // {
            //     客房订单表 orderDetail = new 客房订单表();
            //     orderDetail.购物车编号 = cart.购物车编号;
            //     orderDetail.客户编号 = cart.用户编号;
            //     orderDetail.客户姓名 = cart.用户姓名;
            //     orderDetail.客房编号 = cart.客房编号;
            //     orderDetail.入住天数 = cart.入住天数.Value.ToString();
            //     orderDetail.入住人数 = cart.入住人数.Value.ToString();
            //     orderDetail.入住时间 = cart.入住日期;
            //     orderDetail.入住经办员编号 = (Login.userid ?? Form1.userid);
            //     orderDetail.订单总价 = cart.订单总价.Value.ToString();
            //     orderDetail.订单日期 = DateTime.Now;

            //     //添加到数据库
            //     db.客房订单表.InsertOnSubmit(orderDetail);


            //     // 从用户购物车表中删除已经生成订单的记录
            //     db.用户购物车表.DeleteOnSubmit(cart);
            // }

            // // 将订单添加到数据库
            // db.客房订单表.InsertOnSubmit(order);
            // db.SubmitChanges();


            //添加信息到订单表
            
            //将购物车中的信息添加到订单表中
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string str = "insert into 客房订单表(购物车编号,客户编号,客户姓名,客房编号,入住天数,入住人数,入住时间,入住经办员编号,订单总价,订单日期,订单状态) values('"
                + dataGridView1.Rows[i].Cells["购物车编号"].Value.ToString() + "','"   //购物车编号
                + dataGridView1.Rows[i].Cells["用户编号"].Value.ToString() + "','"   //客户编号
                + dataGridView1.Rows[i].Cells["用户姓名"].Value.ToString() + "','"   //客户姓名
                + dataGridView1.Rows[i].Cells["客房编号"].Value.ToString() + "','"   //客房编号
                + dataGridView1.Rows[i].Cells["入住天数"].Value.ToString() + "','"   //入住天数
                + dataGridView1.Rows[i].Cells["入住人数"].Value.ToString() + "','"   //入住人数
                + dataGridView1.Rows[i].Cells["入住日期"].Value.ToString() + "','"   //入住时间
                + dataGridView1.Rows[i].Cells["入住经办员编号"].Value.ToString() + "','"   //入住经办员编号
                + dataGridView1.Rows[i].Cells["订单总价"].Value.ToString() + "','"   //订单总价
                + DateTime.Now.ToString()
                + "','未审核'); update 用户购物车表 set 结算状态='已结算' where 购物车编号='" + dataGridView1.Rows[i].Cells["购物车编号"].Value.ToString() + "'";   //订单状态
                DB.GetCn();
                DB.sqlEx(str);

            }
            DB.cn.Close();

            MessageBox.Show("订单已提交");
            this.Dispose();
            roin d1 = new roin();
            d1.ShowDialog();
        }
    }
}
